package tmproject.hlhj.fhp.cartrans.utils

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.graphics.Paint
import android.net.Uri
import android.os.Build
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient

object WebUtils {
    @SuppressLint("SetJavaScriptEnabled", "WrongConstant")
    fun initWeb(webView: WebView, context: Context){
        val webSettings = webView.settings
        if (Build.VERSION.SDK_INT >= 19) {
            webSettings.setCacheMode(1)
        }

        webSettings.loadsImagesAutomatically = Build.VERSION.SDK_INT >= 19

        if (Build.VERSION.SDK_INT >= 11) {
            webView.setLayerType(1, null as Paint?)
        }

        webView.setLayerType(2, null as Paint?)
        webSettings.javaScriptEnabled = true
        webSettings.setSupportZoom(true)
        webSettings.builtInZoomControls = true
        webSettings.displayZoomControls = false
        webSettings.useWideViewPort = true
        webSettings.layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN
        webSettings.loadWithOverviewMode = true
        webSettings.databaseEnabled = true
        webSettings.savePassword = true
        webSettings.domStorageEnabled = true
        webView.isSaveEnabled = true
        webView.keepScreenOn = true
        webView.webViewClient = object : WebViewClient() {
            override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
                if (!url.startsWith("http:") && !url.startsWith("https:")) {
                    try {
                        val intent = Intent("android.intent.action.VIEW", Uri.parse(url))
                        context.startActivity(intent)
                        return true
                    } catch (e: Exception) {

                    } finally {
                        return true
                    }
                } else {
                    view.loadUrl(url)
                    return false
                }
            }
        }
    }

    fun loadUrl(webView: WebView,content:String){
        webView.loadDataWithBaseURL(null,content,"text/html", "utf-8",null)
    }
}